package queue;

public class LinkQueue <T>{

    private Node front;//队头
    private Node rear;//队尾
    private int size;

    public LinkQueue(){
        front = null;
        rear = null;
        size = 0;
    }

    /**
     * 入队
     */
    public void enQueue(T data) {
        Node node = new Node(data, null);
        if (isEmpty()){
            front = node;
        }else {
            rear.next = node;
        }
        rear = node;
        ++size;
    }

    /**
     * 出队
     */
    public T deQueue() {
        if (isEmpty()){
            return null;
        }
        Node node = front;
        front = front.next;
        node.next = null;
        --size;
        return node.data;
    }

    public boolean isEmpty() {
        return size == 0;
    }

    public int size() {
        return size;
    }

    @Override
    public String toString() {
        if (isEmpty()){
            return "[]";
        }
        StringBuilder str = new StringBuilder();
        Node node = front;
        while (node!=null) {
            if (!str.toString().isEmpty()){
                str.append(",");
            }
            str.append(node.data);
            node = node.next;
        }
        str.insert(0, "[").append(']');
        return str.toString();
    }

    class Node{

        T data;
        Node next;

        public Node() {
        }

        public Node(T data, Node next) {
            this.data = data;
            this.next = next;
        }
    }
}
